from configparser import ConfigParser
from app.models import UserInformation, ChargingDetail, ChargingPileReport
from app.api.memory_class import ChargingPileState, AllCarInformation
from app import create_app, db
import os
import json
from config import base_dir
global GLOBAL_DICT
GLOBAL_DICT = {}

""" init api data structure here
"""


def set_value(key, value):
    """ 定义一个全局变量 """
    GLOBAL_DICT[key] = value


def get_value(key, defValue=None):
    try:
        return GLOBAL_DICT[key]
    except:
        return KeyError

app = create_app()
app.app_context().push()
db.create_all()

# cf = ConfigParser()
# cf.read(os.path.join(base_dir, "strategy.cfg"))
file = open(os.path.join(base_dir, "cf.json"), "rb")
fileJson = json.load(file)
# 充电桩故障时调度策略,priority和time
# set_value("strategy", cf.get('system_strategy', "strategy"))
set_value("strategy", fileJson["strategy"])
set_value("waiting_time", fileJson["waiting_time"])
set_value("F_speed", fileJson["F_speed"])
set_value("T_speed", fileJson["T_speed"])
set_value("unit_service_cost", fileJson["unit_service_cost"])
set_value("user_log_in", [])  # 储存登录的用户的信息
set_value("total_space", fileJson["total_space"])  # 储存等候区空间大小
set_value("charging_space", fileJson["charging_space"])  # 储存充电区队列大小
set_value("free_space",  fileJson["free_space"])  # 储存等候区剩余空间大小
set_value("car_information", [])  # 前来排过队的车辆信息
set_value("time_quantum", fileJson["time_quantum"])  # 分段计费的时间段
set_value("time_quantum_cost", fileJson["time_quantum_cost"])  # 分段计费的计费
set_value("day_cost", fileJson["day_cost"])  # 一整天的花费

set_value("pile_car", {"pile1": [], "pile2": [],
                       "pile3": [], "pile4": [], "pile5": []})
# 初始化充电桩在本次系统启动期间的状态
charging_pile_state = []
for i in range(5):
    temp = ChargingPileState(i+1, 1, 0, 0, 0)
    charging_pile_state.append(temp)
set_value("charging_pile_state", charging_pile_state)  # 充电桩在本次系统启动期间的状态

# 删除之前存储的未完成订单的车辆的信息
temp = ChargingDetail.query.filter(
    ChargingDetail.complete == "no").delete()
db.session.commit()
